﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Timers;
using MonitorServer;

namespace MonitorHost
{
    class Program
    {
        static Timer timer;
        static object mylock = new object();

        static void TimerInit()
        {
            timer = new Timer(10);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }
        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (mylock)
            {
                MessageQueueService service = MessageQueueService.CrateInstance();
                string result = service.RequestSave();
                //service.ActionSave();
                if (!string.IsNullOrEmpty(result))
                    Console.WriteLine(result);
            }
            System.Threading.Thread.Sleep(1000);
        }
        static void Main(string[] args)
        {
            try
            {
                Host.StartService();
                TimerInit();
                Console.WriteLine("Server Started.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            while (true)
            {
                Console.WriteLine("Please input 'stop' to close");
                string key = Console.ReadLine();
                if (key == "stop")
                {
                    timer.Stop();
                    Host.StopService();
                    break;
                }
            }
        }

    }
}
